var Flyout = {};
Flyout.timeout = null;

//Is called, when the silverlight-object from the flyout has been created.
//Stores a reference of the flyout-rootelement in the global variable "flyoutSL"
Flyout.init = function(control, userContext, rootElement){
    flyoutSL = rootElement;
    var choice = MSNWidget.platformConnector.loadSettings("bgColor");
    if(choice == false)choice = 0;
    var bg = flyoutSL.findName("FlyoutBg");
    bg.Fill = config.bg_color[choice];
    MSNWidget.token_flyout = flyoutSL.findName("PreviewLinkText").addEventListener("MouseLeftButtonDown", Flyout.openLink);
    flyoutSL.findName("FlyoutDescription").addEventListener("MouseLeftButtonDown", Flyout.openLink);
    flyoutSL.findName("FlyoutImage").addEventListener("MouseLeftButtonDown", Flyout.openLink);
    
    //Create the flyout-weathersearchbar
    Flyout.weatherSearchBar = new WeatherSearchBar();
    Flyout.weatherSearchBar.init(flyoutSL);
    
    //Create the flyout-weatherscrollbar
    Flyout.scrollbar = new Scrollbar();
    Flyout.scrollbar.init(flyoutSL);
    
    //Initialize the localisation
    flyoutSL.findName("txtLabel1").Text = locale.weather.startSearch;
    
    //Align the "loading"-string vertically & horizontally
    var ldngTxt = flyoutSL.findName("WeatherLoadingTxt");
    ldngTxt.Text = locale.weather.loading;
    ldngTxt["Canvas.Top"] = (flyoutSL.Height / 2) - ( ldngTxt.ActualHeight /2 );
    ldngTxt["Canvas.Left"] = (flyoutSL.Width / 2) - ( ldngTxt.ActualWidth /2 );
    
}

//Shows all the necessary information of the active newsitem 
Flyout.addContentToFlyout = function() {

    if (!MSNWidget.platformConnector.flyoutShow())return;
    
    flyoutSL.findName("WeatherDisplay").Visibility = "Collapsed";
    flyoutSL.findName("FlyoutContent").Visibility = "Visible";
    flyoutSL.findName("WeatherChangeLocationText").Visibility = "Collapsed";

    flyoutSL.findName("PreviewLinkText").Text = (News.isVideo) ? locale.flyout.see_more : locale.news.readmore;
    flyoutSL.findName("PreviewLinkText_bg").Width = flyoutSL.findName("PreviewLinkText").ActualWidth + 12;

    var height = flyoutSL.findName("FlyoutBg").Height - flyoutSL.findName("FlyoutBar").Height - 50;
    
    //If the title-animation is runnining, stop it.
    flyoutSL.findName("ScrollTitle").stop();

    flyoutSL.findName("FlyoutTitle").Text = "-  " + MSNWidget.preview_title[MSNWidget.news].replace(/&amp;/g, "&") + "  -  ";
    flyoutSL.findName("FlyoutTitle")["Canvas.Left"] = -6;
    flyoutSL.findName("FlyoutTitle2").Text = MSNWidget.preview_title[MSNWidget.news].replace(/&amp;/g, "&");;
    flyoutSL.findName("FlyoutTitle2")["Canvas.Left"] = flyoutSL.findName("FlyoutTitle").ActualWidth - 6;
    
    //Set the news-image and its reflection
    if (MSNWidget.preview_img[MSNWidget.news] != ""){
        flyoutSL.findName("FlyoutImage").Source = MSNWidget.preview_img[MSNWidget.news];
        flyoutSL.findName("FlyoutImage_refl").Source = MSNWidget.preview_img[MSNWidget.news];
        if(flyoutSL.findName("FlyoutImage").Height == 0){
            window.setTimeout("Flyout.adjustReflection()", 10);
        } else {
            flyoutSL.findName("FlyoutImage_refl")["Canvas.Top"] = flyoutSL.findName("FlyoutImage").Height + flyoutSL.findName("FlyoutImage")["Canvas.Top"];
        }
    }
    
    //Set the news-text
    if (MSNWidget.preview[MSNWidget.news] == "") flyoutSL.findName("FlyoutDescription").Text = locale.flyout.no_description;
    else {
        var text = MSNWidget.preview[MSNWidget.news];
        flyoutSL.findName("FlyoutDescription").Text = text;
        while(flyoutSL.findName("FlyoutDescription").ActualHeight > height) {
            text = text.substring(0, text.length-5)+"...";
            flyoutSL.findName("FlyoutDescription").Text = text;
        }
    }
        
    if(Flyout.timeout != null)
        window.clearTimeout(Flyout.timeout);
    Flyout.timeout = window.setTimeout("Flyout.scrollTitle()", 500);
    
    MSNWidget.platformConnector.makePageView(MSNWidget.news);
}

Flyout.adjustReflection = function(){
    if(typeof(flyoutSL.findName("FlyoutImage")) == "undefined")
        return;
        
    var img_obj = flyoutSL.findName("FlyoutImage");
    var img_refl = flyoutSL.findName("FlyoutImage_refl");
    if(img_obj.Height == 0 || img_refl.Height == 0){
        window.setTimeout("Flyout.adjustReflection()", 10);
        return;
    } else {
        img_refl["Canvas.Top"] = img_obj.Height + img_obj["Canvas.Top"];
    }
}

//Opens the MSN-page
Flyout.openLink = function(sender, eventArgs){
    if(Weather.visible)
        Weather.openForecast(null, null);
    else
        window.open(MSNWidget.preview_link[MSNWidget.news]);
}


//Scrolls the title across the headline
Flyout.scrollTitle = function(){
    if(!MSNWidget.platformConnector.flyoutShow())
        return;
    var obj_title = flyoutSL.findName("FlyoutTitle");
    var obj_canv = flyoutSL.findName("FlyoutBg");
    var animation = flyoutSL.findName("ScrollTitle");
    var distance = obj_title.ActualWidth;
    var minutes = 0;
    var seconds = Math.ceil(distance / 30);
    if(seconds > 60){
        seconds = seconds % 60;
        minutes = Math.floor(seconds / 60);
    } 
    animation.stop();
    animation.children.getItem(0).KeyFrames.getItem(0).Value = -6;
    animation.children.getItem(0).KeyFrames.getItem(1).Value = (-1)*(distance);
    animation.children.getItem(0).KeyFrames.getItem(1).KeyTime = "0:"+minutes+":"+seconds;
    animation.children.getItem(1).KeyFrames.getItem(0).Value = distance -6;
    animation.children.getItem(1).KeyFrames.getItem(1).Value = 8;
    animation.children.getItem(1).KeyFrames.getItem(1).KeyTime = "0:"+minutes+":"+seconds;
    animation.begin();
}

//Shows the 4-days weather-forecast in the flyout
Flyout.showWeather = function(){

    flyoutSL.findName("WeatherDisplay").Visibility = "Visible";
    flyoutSL.findName("WeatherChangeLocationText").Visibility = "Visible";
    flyoutSL.findName("FlyoutContent").Visibility = "Collapsed";
    flyoutSL.findName("PreviewLinkText").Text = locale.weather.flyout_btn;
    flyoutSL.findName("WeatherChangeLocationText").Text = locale.weather.changeLocation_docked;
    flyoutSL.findName("PreviewLinkText_bg").Width = flyoutSL.findName("PreviewLinkText").ActualWidth + 12;
    
    if(MSNWidget.token_flyout == null){
        MSNWidget.token_flyout = flyoutSL.findName("PreviewLinkText").addEventListener("MouseLeftButtonDown", Flyout.openLink);
        flyoutSL.findName("FlyoutImage").addEventListener("MouseLeftButtonDown", Flyout.openLink);
    }

    flyoutSL.findName("FlyoutTitle").Text = "- "+ locale.weather.flyout_headline + Weather.current.location+" - ";
    flyoutSL.findName("FlyoutTitle2").Text = locale.weather.flyout_headline + Weather.current.location;

    for(var i = 0; i < Weather.forecast.length; i++){
        if(Weather.forecast[i].weatherIcon.sunMoonImg)
		    flyoutSL.findName("Day_"+i+"_SunMoonIcon").Source = Weather.imagePath + Weather.forecast[i].weatherIcon.sunMoonImg;
	    else flyoutSL.findName("Day_"+i+"_SunMoonIcon").Source = "";
		
	    if(Weather.forecast[i].weatherIcon && Weather.forecast[i].weatherIcon.sunMoonRotation)
		    flyoutSL.findName("Day_"+i+"_SunMoonIcon_Rotation").ScaleX = Weather.forecast[i].weatherIcon.sunMoonRotation;
	    else flyoutSL.findName("Day_"+i+"_SunMoonIcon_Rotation").ScaleX= 1;
		
	    if(Weather.forecast[i].weatherIcon.weatherImg.length > 0)
		    flyoutSL.findName("Day_"+i+"_WeatherIcon").Source = Weather.imagePath + Weather.forecast[i].weatherIcon.weatherImg;
	    else flyoutSL.findName("Day_"+i+"_WeatherIcon").Source="";
		
	    if(Weather.forecast[i].skytextday){
		    flyoutSL.findName("Day_"+i+"_Conditions").Text = Weather.forecast[i].skytextday;
		    flyoutSL.findName("Day_"+i+"_Conditions").TextWrapping="Wrap";
	    } else {
		    flyoutSL.findName("Day_"+i+"_Conditions").Text = locale.weather.notAvailable;
		    flyoutSL.findName("Day_"+i+"_Conditions").TextWrapping="Wrap"
	    }
		
	    if(Weather.forecast[i].high)
		    flyoutSL.findName("Day_"+i+"_Hi").Text = Weather.forecast[i].high +unescape("%B0")+ Weather.degreetype;
	    else flyoutSL.findName("Day_"+i+"_Hi").Text = "";
		
	    if(Weather.forecast[i].low)
		    flyoutSL.findName("Day_"+i+"_Lo").Text = Weather.forecast[i].low +unescape("%B0")+ Weather.degreetype;
	    else flyoutSL.findName("Day_"+i+"_Lo").Text = "";
		
	    if(Weather.forecast[i].day)
		    flyoutSL.findName("Day_"+i+"_Day").Text = Weather.forecast[i].day;
	    else flyoutSL.findName("Day_"+i+"_Day").Text = "";
   }
   Flyout.scrollTitle();
}

Flyout.stopTitle = function(){
    var animation = flyoutSL.findName("ScrollTitle");
    var animation2 = flyoutSL.findName("ScrollTitle2");
    animation.stop();
    animation2.stop();
}